// import com.alibaba.druid.DbType_ptr;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor_ptr;
// import com.alibaba.druid.util.FnvHash;

// import java.sql.Types;
// import java.util.ArrayList;
// import java.util.Collections;
// import java.util.List;

#include "SQLStructDataType.h"
#include "../visitor/SQLASTVisitor.h"
#include "../../utils/FnvHash.h"
#include "../../Exception/UnsupportedOperationException.h"
#include "../../utils/Types.h"
#include "../../utils/listTran.h"
#include "SQLName.h"

SQLStructDataType_Field::SQLStructDataType_Field(SQLName_ptr name, SQLDataType_ptr dataType)
{
  setName(name);
  setDataType(dataType);
}

// @Override
void SQLStructDataType_Field::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLStructDataType_Field)))
  {
    acceptChild(visitor, name);
    acceptChild(visitor, dataType);
  }
  visitor->endVisit(SharedObject(SQLStructDataType_Field));
}

SQLName_ptr SQLStructDataType_Field::getName()
{
  return name;
}

void SQLStructDataType_Field::setName(SQLName_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLStructDataType_Field));
  }
  this->name = x;
}

SQLDataType_ptr SQLStructDataType_Field::getDataType()
{
  return dataType;
}

void SQLStructDataType_Field::setDataType(SQLDataType_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLStructDataType_Field));
  }
  this->dataType = x;
}

string_ptr SQLStructDataType_Field::getComment()
{
  return comment;
}

void SQLStructDataType_Field::setComment(string_ptr comment)
{
  this->comment = comment;
}

SQLStructDataType::SQLStructDataType()
{
  fields = std::make_shared<std::list<SQLStructDataType_Field_ptr>>();
}

SQLStructDataType::SQLStructDataType(DbType_ptr dbType)
{
  this->dbType = dbType;
  fields = std::make_shared<std::list<SQLStructDataType_Field_ptr>>();
}

// @Override
string_ptr SQLStructDataType::getName()
{
  return make_string_ptr("STRUCT");
}

// @Override
long SQLStructDataType::nameHashCode64()
{
  return FnvHash::Constants::STRUCT;
}

// @Override
void SQLStructDataType::setName(string_ptr name)
{
  throw new UnsupportedOperationException();
}

// @Override
SQLExpr_list_ptr SQLStructDataType::getArguments()
{
  SQLExpr_list_ptr tmp = std::make_shared<std::list<SQLExpr_ptr>>();
  return tmp;
}

// @Override
BOOL_ptr SQLStructDataType::getWithTimeZone()
{
  return BOOL::FALSE;
}

// @Override
void SQLStructDataType::setWithTimeZone(BOOL_ptr value)
{
  throw new UnsupportedOperationException();
}

// @Override
BOOL_ptr SQLStructDataType::isWithLocalTimeZone()
{
  return BOOL::FALSE;
}

// @Override
void SQLStructDataType::setWithLocalTimeZone(BOOL_ptr value)
{
  throw new UnsupportedOperationException();
}

// @Override
void SQLStructDataType::setDbType(DbType_ptr dbType)
{
  this->dbType = dbType;
}

// @Override
DbType_ptr SQLStructDataType::getDbType()
{
  return dbType;
}

// @Override
void SQLStructDataType::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLStructDataType)))
  {
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLStructDataType_Field, SQLObject>(fields);
    acceptChild(visitor, tmp1);
  }
  visitor->endVisit(SharedObject(SQLStructDataType));
}

SQLObject_ptr SQLStructDataType::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLStructDataType>(new SQLStructDataType(dbType));
  std::shared_ptr<SQLStructDataType> x = std::dynamic_pointer_cast<SQLStructDataType>(clone_tmp);
  // SQLStructDataType x = new SQLStructDataType(dbType);

  for (SQLStructDataType_Field_ptr field : *fields)
  {
    x->addField(field->name, std::dynamic_pointer_cast<SQLDataType>(field->dataType->clone()));
  }

  return x;
}

SQLStructDataType_Field_list_ptr SQLStructDataType::getFields()
{
  return fields;
}

SQLStructDataType_Field_ptr SQLStructDataType::addField(SQLName_ptr name, SQLDataType_ptr dataType)
{
  SQLStructDataType_Field_ptr field = SQLStructDataType_Field_ptr(new SQLStructDataType_Field(name, dataType));
  field->setParent(SharedObject(SQLStructDataType));
  fields->push_back(field);
  return field;
}

int SQLStructDataType::jdbcType()
{
  return Types::STRUCT;
}

// @Override
BOOL_ptr SQLStructDataType::isInt()
{
  return BOOL::FALSE;
}

// @Override
BOOL_ptr SQLStructDataType::isNumberic()
{
  return BOOL::FALSE;
}

// @Override
BOOL_ptr SQLStructDataType::isString()
{
  return BOOL::FALSE;
}

// @Override
BOOL_ptr SQLStructDataType::hasKeyLength()
{
  return BOOL::FALSE;
}
